#!/usr/bin/perl
# 
#  Exercise 7.8 
# 
# This is a more difficult exercise. The study function in Perl may speed up searches 
# for motifs in DNA or protein. Read the Perl documentation on this function. Its use 
# is simple: given some sequence data in a variable $sequence, type:
# 
# study $sequence;
# 
# before doing the searches. Do you think study will speed up searches in DNA or 
# protein, based on what you've read about it in the documentation?
# 
# For lots of extra credit! Now read the Perl documentation on the standard module 
# Benchmark. (Type perldoc Benchmark, or visit the Perl home page at http://www.
# perl.com.) See if your guess is right by writing a program that benchmarks motif 
# searches of DNA and of protein, with and without study.
#
# Answer to Exercise 7.8

use strict;
use warnings;

use Benchmark;

my $dna = join ('', qw(
agatggcggcgctgaggggtcttgggggctctaggccggccacctactgg
tttgcagcggagacgacgcatggggcctgcgcaataggagtacgctgcct
gggaggcgtgactagaagcggaagtagttgtgggcgcctttgcaaccgcc
tgggacgccgccgagtggtctgtgcaggttcgcgggtcgctggcgggggt
cgtgagggagtgcgccgggagcggagatatggagggagatggttcagacc
cagagcctccagatgccggggaggacagcaagtccgagaatggggagaat
gcgcccatctactgcatctgccgcaaaccggacatcaactgcttcatgat
cgggtgtgacaactgcaatgagtggttccatggggactgcatccggatca
ctgagaagatggccaaggccatccgggagtggtactgtcgggagtgcaga
gagaaagaccccaagctagagattcgctatcggcacaagaagtcacggga
gcgggatggcaatgagcgggacagcagtgagccccgggatgagggtggag
ggcgcaagaggcctgtccctgatccagacctgcagcgccgggcagggtca
gggacaggggttggggccatgcttgctcggggctctgcttcgccccacaa
atcctctccgcagcccttggtggccacacccagccagcatcaccagcagc
agcagcagcagatcaaacggtcagcccgcatgtgtggtgagtgtgaggca
tgtcggcgcactgaggactgtggtcactgtgatttctgtcgggacatgaa
gaagttcgggggccccaacaagatccggcagaagtgccggctgcgccagt
gccagctgcgggcccgggaatcgtacaagtacttcccttcctcgctctca
ccagtgacgccctcagagtccctgccaaggccccgccggccactgcccac
ccaacagcagccacagccatcacagaagttagggcgcatccgtgaagatg
agggggcagtggcgtcatcaacagtcaaggagcctcctgaggctacagcc
acacctgagccactctcagatgaggaccta
));

my $protein = join('', qw(
MNIDDKLEGLFLKCGGIDEMQSSRTMVVMGGVSGQSTVSGELQD
SVLQDRSMPHQEILAADEVLQESEMRQQDMISHDELMVHEETVKNDEEQMETHERLPQ
GLQYALNVPISVKQEITFTDVSEQLMRDKKQIR
));

my $count = 1000;

print "DNA pattern matches without 'study' function:\n";
timethis($count,
	' for(my $i=1 ; $i < 10000; ++$i) {
		$dna =~ /aggtc/;
		$dna =~ /aatggccgt/;
		$dna =~ /gatcgatcagctagcat/;
		$dna =~ /gtatgaac/;
		$dna =~ /[ac][cg][gt][ta]/;
		$dna =~ /ccccccccc/;
	} '
);

print "\nDNA pattern matches with 'study' function:\n";
timethis($count,
	' study $dna;
	for(my $i=1 ; $i < 10000; ++$i) {
		$dna =~ /aggtc/;
		$dna =~ /aatggccgt/;
		$dna =~ /gatcgatcagctagcat/;
		$dna =~ /gtatgaac/;
		$dna =~ /[ac][cg][gt][ta]/;
		$dna =~ /ccccccccc/;
	} '
);

print "\nProtein pattern matches without 'study' function:\n";
timethis($count,
	' for(my $i=1 ; $i < 10000; ++$i) {
		$protein =~ /PH.EI/;
		$protein =~ /KFTEQGESMRLY/;
		$protein =~ /[YAL][NVP][ISV][KQE]/;
		$protein =~ /DKKQIR/;
		$protein =~ /[MD][VT][HQ][ER]/;
		$protein =~ /NVPISVKQEITFTDVSEQL/;
	} '
);

print "\nProtein pattern matches with 'study' function:\n";
timethis($count,
	' study $protein;
	for(my $i=1 ; $i < 10000; ++$i) {
		$protein =~ /PH.EI/;
		$protein =~ /KFTEQGESMRLY/;
		$protein =~ /[YAL][NVP][ISV][KQE]/;
		$protein =~ /DKKQIR/;
		$protein =~ /[MD][VT][HQ][ER]/;
		$protein =~ /NVPISVKQEITFTDVSEQL/;
	} '
);

# On my computer, this is the output I get: your results probably vary.

#  $ perl exer07.08
#  DNA pattern matches without 'study' function:
#  timethis 1000: 29 wallclock secs (29.25 usr +  0.00 sys = 29.25 CPU) @ 34.19/s (n=1000)
#  
#  DNA pattern matches with 'study' function:
#  timethis 1000: 30 wallclock secs (29.21 usr +  0.15 sys = 29.36 CPU) @ 34.06/s (n=1000)
#  
#  Protein pattern matches without 'study' function:
#  timethis 1000: 32 wallclock secs (29.47 usr +  0.04 sys = 29.51 CPU) @ 33.89/s (n=1000)
#  
#  Protein pattern matches with 'study' function:
#  timethis 1000: 30 wallclock secs (28.97 usr +  0.02 sys = 28.99 CPU) @ 34.49/s (n=1000)
#  
